package com.ruoyi.user.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Maps;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.vo.UserInfoVo;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.user.domain.UserInfo;
import com.ruoyi.user.domain.vo.UserCompareVo;
import com.ruoyi.user.mapper.UserInfoMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Description:
 * Author: YangShiWei
 * Email: strongwalter2014@gmail.com
 * Datetime: 2023/2/14 13:10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/user/compare")
public class UserCompareController {

    @PostMapping("/exportCompareTpl")
    public void export(HttpServletResponse response) {
        List<UserCompareVo> list = Lists.newArrayList();
        ExcelUtil.exportExcel(list, "社戒社康人员数据对比模版", UserCompareVo.class, response);
    }

    private final UserInfoMapper userInfoMapper;


    @PostMapping(value = "/uploadToCompare", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Collection<UserCompareVo>> upload(@RequestPart("file") MultipartFile file) throws IOException {
        if (ObjectUtil.isNull(file)) {
            throw new ServiceException("上传文件不能为空");
        }
        ExcelResult<UserCompareVo> result = ExcelUtil.importExcel(file.getInputStream(), UserCompareVo.class, false);
        List<UserCompareVo> data = result.getList();
        Set<String> namesSet = data.stream().map(d -> d.getName()).collect(Collectors.toSet());
        Set<String> idnoSSet = data.stream().map(d -> d.getIdno()).collect(Collectors.toSet());
        if (CollUtil.isEmpty(namesSet) && CollUtil.isEmpty(idnoSSet)) {
            return R.ok(data);
        }

        Map<String, UserCompareVo> dataMap = Maps.newHashMap();
        data.forEach(d -> {
            dataMap.put(d.getName() + d.getIdno(), d);
        });
        List<UserInfoVo> userInfoList = userInfoMapper.selectVoList(Wrappers.<UserInfo>lambdaQuery().in(CollUtil.isNotEmpty(namesSet), UserInfo::getName, namesSet).or().in(CollUtil.isNotEmpty(idnoSSet), UserInfo::getIdno, idnoSSet));
        userInfoList.forEach(d -> {
            String key = d.getName() + d.getIdno();
            if (dataMap.containsKey(key)) {
                dataMap.get(d.getName() + d.getIdno()).setExistUserId(d.getId());
                dataMap.get(d.getName() + d.getIdno()).setUserBusinessSubType(d.getSubType());
            }
        });
        return R.ok(dataMap.values());
    }

}
